// add/shareHelp/index/index.js
var app = getApp();
var core = app.requirejs('core');
var util = require('../../../utils/util.js')
const api = require("../../../utils/api.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    indicatorDots: false,
    autoplay: false,
    interval: 3000,
    duration: 1000,
    max: 1,
    currentTab: 0,
    rule: false,
    helpfail: false,
    helpsuccess: false,
    loading: true,
    show: true, //页面内容是否显示
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let $this = this
    let pageview = util.getCurrentPageUrl()
    if (options.shareMemberId) {
      app.setCache('currentUrl', 'add/shareHelp/index/index?id=' + options.id + '&shareMemberId=' + options.shareMemberId)
    } else {
      app.setCache('currentUrl', 'add/shareHelp/index/index?id=' + options.id)
    }
    if (options) {
      this.setData({
        options
      })
    }
    this.selShareList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    let uid = app.getCache('userinfo').id
    let url = '/add/shareHelp/index/index?id=' + this.data.data.id + '&shareMemberId=' + this.data.data.shareMemberId
    return core.onShareAppMessage(url, this.data.data.shareTitle, this.data.data.shareImage);
  },
  /**
   * 图片自适应
   */
  loadpic: function(event) {

    let picAry = pickOption.specs
    if (!picAry) {
      return false
    }
    let picitems = picAry[pindex].items
    let picWidth = event.detail.width
    let picHeight = event.detail.height
    picitems[index].styleCss = {
      width: picWidth,
      height: picHeight
    }
    this.setData({
      pickerOption: pickOption
    })
  },
  //查询活动
  selShareList() {
    // 查询活动
    let apicontent = api.markings.selShareList()
    let $this = this
    let options = this.data.options
    core.post(apicontent, {
      id: $this.data.options.id
    }, function(res) {
      if (res.code == 1) {
        wx.setNavigationBarTitle({
          title: res.data.activityName
        })
        $this.setData({
          loading: false
        })
      } else {
        wx.showModal({
          title: '提示',
          content: res.message,
          showCancel: false,
          success: function(res) {
            wx.navigateBack({
              delta: 1,
            })
          },
          fail: function(res) {},
          complete: function(res) {},
        })
      }
      res.data.shufflingPicture = res.data.shufflingPicture.split(",")
      res.data.activityRule = res.data.activityRule.split("br")
      let friend = []
      for (let i = 0; i < res.data.helpNumber; i++) {
        let item = {}
        friend.push(item)
      }

      $this.setData({
        data: res.data,
        friend
      })
      if (options.shareMemberId) {
        let isUid = options.shareMemberId.includes('undefined') ? true : false
        if (options.shareMemberId && options.shareMemberId != res.data.shareMemberId && isUid == false) {

          $this.helperSetUp()
        }
      }
      if (res.data.isConfirm == 1 && res.data.helpNumber <= res.data.memberList.length) {
        $this.setData({
          shareuser: true
        })
      }
      if (res.data.isConfirm == 0 && res.data.helpNumber <= res.data.memberList.length) {
        $this.updata()
      }
    })
  },
  //助力详情
  selShareDetail() {
    let $this = this
    let apicontent = api.markings.selShareDetail()
    let uid = app.getCache('userinfo').id
    core.post(apicontent, {
      shareMemberId: $this.data.data.shareMemberId,
    }, function(res) {
      if (res.code == 1) {
        $this.setData({
          isPrize: res.data.isPrize,
          helpsuccess: true
        })
      } else {
        $this.setData({
          helpfail: true
        })
      }
    })
  },
  //好友助力
  helperSetUp() {
    let $this = this
    let apicontent = api.markings.helperSetUp()
    let shareMemberId = $this.data.options.shareMemberId
    core.post(apicontent, {
      shareMemberId: $this.data.options.shareMemberId
    }, function(res) {

      if ($this.data.data.activityType == 1) {
        if (res.code == 1 || res.code == 5 || res.code == 2 || res.code == 10) {
          wx.showModal({
            title: '提示',
            content: res.message,
            showCancel: false,
            success: function(res) {},
            fail: function(res) {},
            complete: function(res) {},
          })
          // $this.selShareDetail()
        } else if (res.code == 4) {
          $this.setData({
            helpsuccess: true,
            // code: 4
          })
        } else if (res.code == 5) {
          wx.showModal({
            title: '提示',
            content: res.message,
            showCancel: false,
            success: function(res) {},
            fail: function(res) {},
            complete: function(res) {},
          })
        } else if (res.code == 9) {
          // 本人剩余的助力次数
          let number = res.data.totalPower - res.data.memberTotalAssis
          //本人给A剩余的助力次数
          let numbertwo = res.data.repeatNumber - res.data.memberThisAssis
          let content = ''
          if (numbertwo == 0) {
            content = "重复助力次数已达上限"
          } else if (number == 0) {
            content = "助力次数已达上限"
          } else if (number > 0) {
            content = '助力成功，奖品发放已达上线，剩余助力次数' + number + '次'
          }

          wx.showModal({
            title: '提示',
            content: content,
            showCancel: false,
            success: function(res) {},
            fail: function(res) {},
            complete: function(res) {},
          })
        }
      }
    })
  },
  //提交form表单  //添加fromid+ 分享即得
  formSubmit(e) {
    let $this = this
    let uid = app.getCache('userinfo').id
    // 分享即得
    if ($this.data.activityType == 2) {
      let apicontent = api.markings.shareAndGive()
      core.post(apicontent, {
        shareMemberId: $this.data.data.shareMemberId,
        formId: e.detail.formId
      }, function(res) {})
    }
    if ($this.data.data.isClose != 0) {
      $this.updata()

    }
  },
  //轮播
  prevSwiper(e) {
    this.setData({
      currentTab: this.data.currentTab != 0 ? this.data.currentTab - 1 : this.data.max
    });
  },

  nextSwiper(e) {
    this.setData({
      currentTab: this.data.currentTab < this.data.max ? this.data.currentTab + 1 : 0
    })
  },
  backhome() {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },

  //关闭规则
  clear() {

    this.setData({
      rule: false,
      helpsuccess: false,
      helpfail: false
    })
  },
  //打开规则
  openrule() {
    this.setData({
      rule: true
    })
  },
  //清空
  clearuser() {
    let $this = this
    let apicontent = api.markings.confirm()

    core.post(apicontent, {
      shareMemberId: $this.data.data.shareMemberId
    }, function(rett) {
      if (rett.data == true) {
      }
    })
    this.setData({
      shareuser: false
    })
  },
  // 更新活动
  updata() {
    let $this = this

    if ($this.data.data.isRepeat == 2) {
      return
    }
    let options = this.data.options
    let data = $this.data.data
    if (data.memberNumber == data.memberNumberCount) {
      return
    }
    if (data.countNumber == 3) {
      return
    }

    wx.showModal({
      title: '提示2',
      content: '您的助力位已满，是否进行下一轮',
      showCancel: true,
      success: function (ret) {

        if (ret.cancel == false) {
          let data = $this.data.data
          let apicontent = api.markings.startAssis()
          core.post(apicontent, {
            shareId: $this.data.data.id
          }, function (ress) {
            if (ress.code == 1) {
              options.shareMemberId = ''
              $this.setData({
                options
              })
              $this.selShareList()
            } else {
              wx.showModal({
                title: '提示',
                content: '活动结束',
                showCancel: false,
                success: function (res) { },
                fail: function (res) { },
                complete: function (res) { },
              })
            }
          })
        }
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  }
})